package ru.zombator.taskman.server.query.corba.manual;

import java.net.InetAddress;
import java.rmi.RemoteException;
import java.util.Properties;

import org.omg.CORBA.Any;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableInterceptor.Current;
import org.omg.PortableInterceptor.CurrentHelper;
import org.omg.PortableInterceptor.InvalidSlot;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import ru.zombator.taskman.core.query.IPacket;
import ru.zombator.taskman.core.query.IResponderCli;
import ru.zombator.taskman.server.query.BaseServer;
import ru.zombator.taskman.server.query.corba.ICorbaServer;
import ru.zombator.taskman.server.query.corba.ICorbaServerHelper;

public class CorbaServer extends BaseServer implements IResponderCli {
	private ORB orb;
	
	@Override
	public IPacket getResponse(IPacket request) throws RemoteException {
		Current pic;
		try {
			pic = CurrentHelper.narrow(orb.resolve_initial_references(
					"PICurrent"));
			Any ipaddr = pic.get_slot(ORBIPAddrIntercInitializer.slotId);
			String clientIpAddress = ipaddr.extract_string();
			return getResponse(request, clientIpAddress);
		} catch (InvalidName e) {
			e.printStackTrace();
			throw new RemoteException(e.getMessage(), e);
		} catch (InvalidSlot e) {
			e.printStackTrace();
			throw new RemoteException(e.getMessage(), e);
		}
	}

	public CorbaServer() throws RemoteException {
	    String myIp = "localhost";
	    try {
    	    String hostName = InetAddress.getLocalHost().getHostName();
    	    
    	    InetAddress addrs[] = InetAddress.getAllByName(hostName);
    
    	    for (InetAddress addr: addrs) {
        	    if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress()) {
            	    myIp = addr.getHostAddress();
            	    break;
        	    }
    	    }
	    } catch (Exception e) {
	    }

	    
		String[] args = { "-ORBInitialPort", "1550", "-ORBInitialHost",
				myIp };
		try {
			Properties props = new Properties();
			props
					.put(
							"org.omg.PortableInterceptor.ORBInitializerClass."
									+ "ru.zombator.taskman.server.query.corba.manual.ORBIPAddrIntercInitializer",
							"");
			orb = ORB.init(args, props);

			POA rootpoa = POAHelper.narrow(orb
					.resolve_initial_references("RootPOA"));
			rootpoa.the_POAManager().activate();

			CorbaServerImpl srvImpl = new CorbaServerImpl(this);
			org.omg.CORBA.Object ref = rootpoa.servant_to_reference(srvImpl);
		    ICorbaServer href = ICorbaServerHelper.narrow(ref);
		          
			org.omg.CORBA.Object objRef = orb
					.string_to_object("corbaloc:iiop:localhost:1550/NameService");
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
			final String name = "ICorbaServer";

			NameComponent path[] = ncRef.to_name(name);
			ncRef.rebind(path, href);
		} catch (Exception e) {
			throw new RemoteException(e.getMessage(), e);
		}
	}
}
